﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using SQLServerHealthCheck.SQLServer;
using SQLServerOptimizer.PL.WPF;
using System.Windows.Threading;
using AG.Utilities;

namespace SQLServerOptimizer.PL.WPF
{
    /// <summary>
    /// Interaction logic for DatabaseSetup.xaml
    /// </summary>
    public partial class DatabaseSetup : Window
    {
        string newInstance = "";
       
        public DatabaseSetup()
        {
            InitializeComponent();
            MouseLeftButtonDown += new MouseButtonEventHandler(MainWindow_MouseLeftButtonDown);
            
        }
        private DatabaseInstallWorker progworker;
        progressCircle progcircle;
        void MainWindow_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            DragMove();
        }
        
        public void btnDatabaseSubmit_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                newInstance = txtBoxInstanceSetup.Text;
                
                this.Cursor = Cursors.AppStarting;

                progworker = new DatabaseInstallWorker(this, newInstance);
                progworker.ProgressUpdate += new ProgressBarUpdateEvent(progworker_ProgressUpdate);
                progworker.ProccessCompleted += new ProccesDatabaseSetupCompleted(progworker_ProccessCompleted);

                progworker.Start();
                progcircle = (progressCircle)Grid_progress.FindName("DatabaseSetup_progress");
                progcircle.Visibility = Visibility.Visible;
            }
            catch(Exception ex)
            {
                Logger.Error(ex);
            }
              
                
        }

        private void btnCancel_dbSetup_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void btnClose_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }
        public void progworker_ProgressUpdate(String instance, String error)
        {
            
            this.lblerror.Content = error;
           
            progcircle.Visibility = Visibility.Hidden;
       
        }
        
        
        public void progworker_ProccessCompleted()
        {
            progcircle.Visibility = Visibility.Hidden;
            this.Cursor = Cursors.Arrow;
            this.Close();
        
        }
    
    
    
    }
}
